#include "database.h"

database::database()
{
}

//Opens the player database, if the player database does not exist then it creates one
bool database::openPlayerDatabase()
{
    playerDatabaseConnection = QSqlDatabase::addDatabase("QSQLITE");
    playerDatabaseConnection.setDatabaseName("playerdatabase");
    if (playerDatabaseConnection.open() == false){
        return false;
    }
    query = QSqlQuery(playerDatabaseConnection);
    query.exec("CREATE TABLE IF NOT EXISTS playerdatabase(uniqueIdentifier INT, playerFirstName VARCHAR(255), playerMiddleName VARCHAR(255), playerLastName VARCHAR(255),"
               "jerseyNumber INT, atBats INT, singles INT, doubles INT, triples INT, grandSlams INT, homeruns INT, walks INT, strikeouts INT, runsScored INT, runsBattedIn INT,"
               "stolenBases INT, sacrificedFlys INT, walksPitched INT, earnedRuns INT, inningsPitched DOUBLE, wins INT, losses INT, hitsAllowed INT, atBatsFaced INT, errors INT)");
    return true;
}

//Closes the player database
void database::closePlayerDatabase()
{
    playerDatabaseConnection.close();
    return;
}

//Inserts a basic player into the database
void database::insertPlayer(int Identifier, QString firstName, QString middleName, QString lastName, int jerseyNumber){
    QString string = QString("INSERT INTO playerdatabase VALUES (%1, '%2', '%3', '%4', %5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)").arg(Identifier)
            .arg(firstName) .arg(middleName) .arg(lastName) .arg(jerseyNumber);
    //qDebug() << string;
    query.prepare(string);
    query.exec();
    return;
}

//Takes a class player and insert it into the database
void database::insertPlayer(Player p){
    QString string = QString("INSERT INTO playerdatabase VALUES (0, '%1', '%2', '%3', %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16, %17, %18, %19, %20, %21, %22, %23, %24, %25)")
            .arg(p.getFirstName())
            .arg(p.getMiddleName())
            .arg(p.getLastName())
            .arg(p.getJerseyNumber())
            .arg(p.getPlayerStats().getPriorHistory().getAtBats())
            .arg(p.getPlayerStats().getPriorHistory().getSingles())
            .arg(p.getPlayerStats().getPriorHistory().getDoubles())
            .arg(p.getPlayerStats().getPriorHistory().getTriples())
            .arg(p.getPlayerStats().getPriorHistory().getGrandSlams())
            .arg(p.getPlayerStats().getPriorHistory().getHomeRuns())
            .arg(p.getPlayerStats().getPriorHistory().getWalks())
            .arg(p.getPlayerStats().getPriorHistory().getStrikeouts())
            .arg(p.getPlayerStats().getPriorHistory().getRunsScored())
            .arg(p.getPlayerStats().getPriorHistory().getRunsBattedIn())
            .arg(p.getPlayerStats().getPriorHistory().getStolenBases())
            .arg(0)
            //.arg(p.getPlayerStats().getPriorHistory().getSacrificeFlys())
            .arg(p.getPlayerStats().getPriorHistory().getWalksPitched())
            .arg(p.getPlayerStats().getPriorHistory().getEarnedRuns())
            .arg(p.getPlayerStats().getPriorHistory().getInningsPitched())
            .arg(p.getPlayerStats().getPriorHistory().getStrikeoutsPitched())
            .arg(p.getPlayerStats().getPriorHistory().getWins())
            .arg(p.getPlayerStats().getPriorHistory().getLosses())
            .arg(p.getPlayerStats().getPriorHistory().getHitsAllowed())
            .arg(p.getPlayerStats().getPriorHistory().getAtBatsFaced())
            .arg(p.getPlayerStats().getPriorHistory().getErrors());
    query.prepare(string);
    query.exec();
    return;
}

//This function is used for debugging purposes only
//Prints all the player names from the database
void database::printAllPlayers(){
    query.exec("SELECT * FROM playerdatabase");
    record = query.record();
    qDebug() << "Number of columns: " << record.count();
    int identifier = record.indexOf("uniqueIdentifier");
    int firstName = record.indexOf("playerFirstName");
    int middleName = record.indexOf("playerMiddleName");
    int lastName = record.indexOf("playerLastName");
    int jerseyNumber = record.indexOf("jerseyNumber");
    while(query.next()){
        qDebug() << query.value(identifier).toString();
        qDebug() << query.value(firstName).toString();
        qDebug() << query.value(middleName).toString();
        qDebug() << query.value(lastName).toString();
        qDebug() << query.value(jerseyNumber).toString();
    }
    return;
}
